//
//  ZFDownloadingCell.m
//  滴滴快拍
//
//  Created by dllo on 16/6/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ZFDownloadingCell.h"

@implementation ZFDownloadingCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
    
}


- (void)createView {
    self.fileNameLabel = [[BaseLabel alloc] init];
    [self.contentView addSubview:self.fileNameLabel];
    self.progress = [[UIProgressView alloc] init];
    [self.contentView addSubview:self.progress];
    self.progressLabel = [[BaseLabel alloc] init];
    [self.contentView addSubview:self.progressLabel];
    self.speedLabel = [[BaseLabel alloc] init];
    [self.contentView addSubview:self.speedLabel];
    self.downloadBtn = [BaseButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:self.downloadBtn];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.fileNameLabel.frame = CGRectMake(15, 0, SCREEN_WIDTH - 15, 30);
    self.progress.frame = CGRectMake(15, 80, SCREEN_WIDTH - 50, 3);
    self.progressLabel.frame = CGRectMake(15, 50, (SCREEN_WIDTH - 15) / 2, 30);
    self.speedLabel.frame = CGRectMake(SCREEN_WIDTH - 100 , 50, 100, 30);
    self.downloadBtn.frame = CGRectMake(SCREEN_WIDTH - 50, 70, 50, 20);
    [self.downloadBtn setTitle:@"🕘" forState:UIControlStateNormal];
    [self.downloadBtn setTitle:@"↓" forState:UIControlStateSelected];
    
    [self.downloadBtn addTarget:self action:@selector(clickDownload:) forControlEvents:UIControlEventTouchUpInside];
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

/**
 *  暂停、下载
 *
 *  @param sender UIButton
 */
- (void)clickDownload:(UIButton *)sender {
    //    sender.selected = !sender.selected;
    if (self.downloadBlock) {
        self.downloadBlock(sender);
    }
}

/**
 *  model setter
 *
 *  @param sessionModel sessionModel
 */
- (void)setSessionModel:(ZFSessionModel *)sessionModel
{
    _sessionModel = sessionModel;
    self.fileNameLabel.text = sessionModel.fileName;
    NSUInteger receivedSize = ZFDownloadLength(sessionModel.url);
    NSString *writtenSize = [NSString stringWithFormat:@"%.2f %@",
                             [sessionModel calculateFileSizeInUnit:(unsigned long long)receivedSize],
                             [sessionModel calculateUnit:(unsigned long long)receivedSize]];
    CGFloat progress = 1.0 * receivedSize / sessionModel.totalLength;
    self.progressLabel.text = [NSString stringWithFormat:@"%@/%@ (%.2f%%)",writtenSize,sessionModel.totalSize,progress*100];
    self.progress.progress = progress;
    self.speedLabel.text = @"已暂停";
}


@end
